package com.ruoyi.system.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.SysLeaveMapper;
import com.ruoyi.system.domain.SysLeave;
import com.ruoyi.system.service.ISysLeaveService;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.exception.ServiceException;

/**
 * 请假记录Service业务层处理
 * 
 * @author ruoyi
 */
@Service
public class SysLeaveServiceImpl implements ISysLeaveService 
{
    @Autowired
    private SysLeaveMapper sysLeaveMapper;

    @Autowired
    private ISysUserService userService;

    /**
     * 查询请假记录
     * 
     * @param id 请假记录主键
     * @return 请假记录
     */
    @Override
    public SysLeave selectSysLeaveById(Long id)
    {
        return sysLeaveMapper.selectSysLeaveById(id);
    }

    /**
     * 查询请假记录列表
     * 
     * @param sysLeave 请假记录
     * @return 请假记录
     */
    @Override
    public List<SysLeave> selectSysLeaveList(SysLeave sysLeave)
    {
        return sysLeaveMapper.selectSysLeaveList(sysLeave);
    }

    /**
     * 新增请假记录
     * 
     * @param sysLeave 请假记录
     * @return 结果
     */
    @Override
    public int insertSysLeave(SysLeave sysLeave)
    {
        // 检查时间冲突
        SysLeave query = new SysLeave();
        query.setElderUserId(sysLeave.getElderUserId());
        query.setStatus("1"); // 只检查已通过的请假记录
        List<SysLeave> existingLeaves = sysLeaveMapper.selectSysLeaveList(query);
        
        for (SysLeave existing : existingLeaves) {
            if (isTimeOverlap(sysLeave.getStartTime(), sysLeave.getEndTime(), 
                            existing.getStartTime(), existing.getEndTime())) {
                throw new ServiceException("该时间段内已有请假记录，请重新选择时间");
            }
        }

        // 获取老人信息
        SysUser elder = userService.selectUserById(sysLeave.getElderUserId());
        if (elder != null)
        {
            sysLeave.setElderName(elder.getNickName());
        }
        return sysLeaveMapper.insertSysLeave(sysLeave);
    }

    /**
     * 修改请假记录
     * 
     * @param sysLeave 请假记录
     * @return 结果
     */
    @Override
    public int updateSysLeave(SysLeave sysLeave)
    {
        // 如果是审核操作，不检查时间冲突
        if (sysLeave.getStatus() != null && !"0".equals(sysLeave.getStatus())) {
            return sysLeaveMapper.updateSysLeave(sysLeave);
        }

        // 检查时间冲突（排除当前记录）
        SysLeave query = new SysLeave();
        query.setElderUserId(sysLeave.getElderUserId());
        query.setStatus("1"); // 只检查已通过的请假记录
        List<SysLeave> existingLeaves = sysLeaveMapper.selectSysLeaveList(query);
        
        for (SysLeave existing : existingLeaves) {
            if (!existing.getId().equals(sysLeave.getId()) && // 排除当前记录
                isTimeOverlap(sysLeave.getStartTime(), sysLeave.getEndTime(), 
                            existing.getStartTime(), existing.getEndTime())) {
                throw new ServiceException("该时间段内已有请假记录，请重新选择时间");
            }
        }

        // 获取老人信息
        SysUser elder = userService.selectUserById(sysLeave.getElderUserId());
        if (elder != null)
        {
            sysLeave.setElderName(elder.getNickName());
        }
        return sysLeaveMapper.updateSysLeave(sysLeave);
    }

    /**
     * 批量删除请假记录
     * 
     * @param ids 需要删除的请假记录主键
     * @return 结果
     */
    @Override
    public int deleteSysLeaveByIds(Long[] ids)
    {
        return sysLeaveMapper.deleteSysLeaveByIds(ids);
    }

    /**
     * 删除请假记录信息
     * 
     * @param id 请假记录主键
     * @return 结果
     */
    @Override
    public int deleteSysLeaveById(Long id)
    {
        return sysLeaveMapper.deleteSysLeaveById(id);
    }

    /**
     * 根据老人ID查询请假记录
     * 
     * @param elderUserId 老人ID
     * @return 请假记录列表
     */
    @Override
    public List<SysLeave> selectSysLeaveByElderUserId(Long elderUserId)
    {
        return sysLeaveMapper.selectSysLeaveByElderUserId(elderUserId);
    }

    /**
     * 检查时间是否重叠
     * 
     * @param start1 第一个开始时间
     * @param end1 第一个结束时间
     * @param start2 第二个开始时间
     * @param end2 第二个结束时间
     * @return 是否重叠
     */
    private boolean isTimeOverlap(java.util.Date start1, java.util.Date end1, 
                                java.util.Date start2, java.util.Date end2) {
        return start1.before(end2) && start2.before(end1);
    }
} 